Skip to content

Sort environment variable names when building Docker args in MCP container compiler#1304

Merged
pelikhan merged 5 commits into
mainfrom
copilot/sort-env-variables-in-compiler
Oct 7, 2025
Merged

Sort environment variable names when building Docker args in MCP container compiler#1304
pelikhan merged 5 commits into
mainfrom
copilot/sort-env-variables-in-compiler

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 7, 2025

Problem

The duplicate-detection lock files had non-deterministic variable order changes because environment variables were not sorted when building Docker arguments for container-based MCP servers. Since Go maps have random iteration order, compiling the same workflow multiple times would produce lock files with different -e KEY flag orderings, even though the functionality was identical.

Solution

Added alphabetical sorting of environment variable keys before building the Docker args list in two locations:

  1. pkg/parser/mcp.go - In ParseMCPConfig() when handling container-based MCP configurations
  2. pkg/workflow/mcp-config.go - In getMCPConfig() when transforming container fields to Docker commands

Both locations now collect env var keys into a slice, sort them alphabetically using sort.Strings(), then iterate over the sorted keys to build the args list. This ensures deterministic lock file generation.

Example

Given a workflow with environment variables:

mcp-servers:
  test-server:
    container: example/test:latest
    env:
      ZEBRA_VAR: "z"
      ALPHA_VAR: "a"
      BETA_VAR: "b"

Before: Args list had random order (e.g., [..., "-e", "ZEBRA_VAR", "-e", "ALPHA_VAR", "-e", "BETA_VAR", ...])

After: Args list has consistent alphabetical order ([..., "-e", "ALPHA_VAR", "-e", "BETA_VAR", "-e", "ZEBRA_VAR", ...])

Testing

  • Existing TestMCPEnvVarsAlphabeticallySorted test validates the fix
  • Created deterministic compilation test that compiles the same workflow 5 times and verifies identical output
  • All 59 workflow files recompiled successfully with consistent ordering
  • Full test suite passes (make test and make agent-finish)
Original prompt

Make sure to sort the environment variable names when building the args list in the mcp-servers container compiler code. The duplicate-detection lock files has non-deterministic variable order changes currently.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits October 7, 2025 14:16
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
…s container compiler

This ensures deterministic lock file generation by sorting env var keys alphabetically
before adding them to the docker args list. Fixes non-deterministic variable order in
duplicate-detection lock files.

Changes:
- Added sort.Strings() for env var keys in pkg/parser/mcp.go (ParseMCPConfig)
- Added sort.Strings() for env var keys in pkg/workflow/mcp-config.go (getMCPConfig)
- Both locations now sort env var keys before building docker args list

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Sort environment variable names in mcp-servers args list Sort environment variable names when building Docker args in MCP container compiler Oct 7, 2025
Copilot AI requested a review from pelikhan October 7, 2025 14:37
@pelikhan pelikhan marked this pull request as ready for review October 7, 2025 14:43
@pelikhan pelikhan merged commit 6fc9e2f into main Oct 7, 2025
12 checks passed
@pelikhan pelikhan deleted the copilot/sort-env-variables-in-compiler branch October 7, 2025 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants